package cloud.smalla.exception.handler;

import cloud.smalla.exception.AuthException;
import cloud.smalla.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import static cloud.smalla.constant.HttpCodeConstant.SERVER_ERROR;

@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(AuthException.class)
    public Result<Object> systemExceptionHandler(AuthException e){
        //打印异常信息
        log.error("出现了异常     {}",e.getMessage());
        e.printStackTrace();
        //封装信息
        return new Result(e.getCode(),e.getMessage());

    }

    @ExceptionHandler
    public Result<Object> ExceptionHandler(Exception e){
        //打印异常信息
        log.error("出现异常");
        e.printStackTrace();
        //封装信息
        return new Result(SERVER_ERROR,e.getMessage());
    }
}
